<?php

/**
 * Kao/App.php
 *
 * Kao: PHP Web Framework <http://www.byhuy.com/kao/>.
 * Copyright 2007-2008, Huy Nguyen <http://www.byhuy.com/>.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 */


/**
 * The Kao application handler class
 *
 * Singleton
 *
 * @author Huy Nguyen <http://www.byhuy.com/>
 * @since 0.1
 *
 */

class Kao_Front
{
    /**
     * Singleton instance
     *
     * @var Kao_Front
     */
    protected static $_instance = null;


    /**
     * Router
     */
    protected $_router = null;

    /**
     * Request
     */
    protected $_request = null;

    /**
     * Response
     */
    protected $_response = null;

    /**
     * Dispatcher
     */
    protected $_dispatcher = null;

 

    /**
     * The constructor
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Return the base URL of the app.
     * If not set, guess it!
     *
     * @return string
     */
    public function getBaseUrl()
    {
        if (null === $this->_baseUrl)
        {
            $this->_baseUrl = substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT']));
        }
        return $this->_baseUrl;
    }

    /**
     * Sets the custom base URL.
     *
     * Useful when works without URL rewriting support from web server.
     *
     * @param string
     * @return Kao_Front
     */
    public function setBaseUrl($baseUrl)
    {
        if (!is_string($baseUrl))
        {
            throw new Kao_Front_Exception('The base URL must be a string. Given a ' . gettype($baseUrl) . '.');
        }
        $this->_baseUrl = $baseUrl;
        return $this;
    }


    /**
     * Return the router
     *
     * @return Kao_Router_Abstract
     */
    public function getRouter()
    {
        if (null === $this->_router)
        {
            require_once 'Kao/Router.php';
            $this->_router = new Kao_Router();
        }
        return $this->_router;
    }

    /**
     * Set custom router
     *
     * @return Kao_Front
     */
    public function setRouter(Kao_Router_Abstract $router)
    {
        $this->_router = $router;
        return $this;
    }

    /**
     * Get the Dispatcher
     */
    public function getDispatcher()
    {
        if (null === $this->_dispatcher)
        {
            require_once 'Kao/App/Dispatcher.php';
            $this->_dispatcher = new Kao_App_Dispatcher();
        }
        return $this->_dispatcher;
    }

    /**
     * Set custom dispatcher
     */
    public function setDispatcher(Kao_App_Dispatcher_Abstract $dispatcher)
    {
        $this->_dispatcher = $dispatcher;
        return $this;
    }

    public function serve()
    {
        $router = $this->getRouter();
        $response = new Kao_App_Response();
        
        if (!$request = $router->route($this->getBaseUrl()))
        {
            // 404
            return $response->setResponseCode(404);
        }
        
         
         
        $dispatcher = $this->getDispatcher();
        // G time!
        $dispatcher->dispatch($request, $response);
    }
}
